home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / xlib05.zip / XTEXT.ASM < prev    next >
Assembly Source File  |  1993-08-25  |  13KB  |  447 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XTEXT
  3. ;
  4. ; Point functions all MODE X 256 Color resolutions
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ;
  14. ; egg@dstos3.dsto.gov.au
  15. ; teg@bart.dsto.gov.au
  16. ;-----------------------------------------------------------------------
  17.  
  18.  
  19. include xlib.inc
  20. include xtext.inc
  21.  
  22. .data
  23.  
  24. _FontDriverActive db 0
  25.  
  26.  
  27. _CharHeight   db         0
  28. _CharWidth    db         0
  29. _FontPtr      dw  2 dup (0)
  30. _FirstChar    db         0
  31.  
  32. _UserFontPtr   dw  2 dup (0)
  33. _UserChHeight  db         0
  34. _UserChWidth   db         0
  35. _UserFirstCh   db         0
  36.  
  37.  
  38. F8x8Ptr       dw  2 dup (0)
  39. F8x14Ptr      dw  2 dup (0)
  40.  
  41. ; This is a look up table for the mirror image of a byte eg
  42. ; a byte with the value 11001010 has a corresponding byte in the table
  43. ; 01010011. This is necessary as the VGA rom font bits are the reverse
  44. ; order of what we need for the Mode X. If you know a better-faster way
  45. ; TELL ME!
  46.  
  47. MirrorTable  label byte
  48.     db   0,128, 64,192, 32,160, 96,224, 16,144, 80,208, 48,176,112,240
  49.     db   8,136, 72,200, 40,168,104,232, 24,152, 88,216, 56,184,120,248
  50.     db   4,132, 68,196, 36,164,100,228, 20,148, 84,212, 52,180,116,244
  51.     db  12,140, 76,204, 44,172,108,236, 28,156, 92,220, 60,188,124,252
  52.     db   2,130, 66,194, 34,162, 98,226, 18,146, 82,210, 50,178,114,242
  53.     db  10,138, 74,202, 42,170,106,234, 26,154, 90,218, 58,186,122,250
  54.     db   6,134, 70,198, 38,166,102,230, 22,150, 86,214, 54,182,118,246
  55.     db  14,142, 78,206, 46,174,110,238, 30,158, 94,222, 62,190,126,254
  56.     db   1,129, 65,193, 33,161, 97,225, 17,145, 81,209, 49,177,113,241
  57.     db   9,137, 73,201, 41,169,105,233, 25,153, 89,217, 57,185,121,249
  58.     db   5,133, 69,197, 37,165,101,229, 21,149, 85,213, 53,181,117,245
  59.     db  13,141, 77,205, 45,173,109,237, 29,157, 93,221, 61,189,125,253
  60.     db   3,131, 67,195, 35,163, 99,227, 19,147, 83,211, 51,179,115,243
  61.     db  11,139, 75,203, 43,171,107,235, 27,155, 91,219, 59,187,123,251
  62.     db   7,135, 71,199, 39,167,103,231, 23,151, 87,215, 55,183,119,247
  63.     db  15,143, 79,207, 47,175,111,239, 31,159, 95,223, 63,191,127,255
  64.  
  65. MirrorTableOffs dw         ?
  66. .code
  67.  
  68. ;----------------------------------------------------------------------
  69. ; x_text_init    - Initializes the Mode X text driver and sets the
  70. ;                  default font (VGA ROM 8x8)
  71. ;
  72. ; C caller:
  73. ;
  74. ;  x_text_init()
  75. ;
  76. ; Written by Themie Gouthas
  77. ;----------------------------------------------------------------------
  78. _x_text_init proc
  79.   push bp
  80.  
  81.   mov  [_FontDriverActive],TRUE
  82.   mov  ax,1130h                   ; AH = BIOS generator function
  83.                   ; AL = BIOS get font pointer subfunction
  84.   push ax                         ; Save Video interrupt function parameters
  85.   mov  bh,3                       ; Select 8x8 VGA ROM font
  86.   int  10h                        ; Call BIOS video interrupt
  87.   mov  word ptr [F8x8Ptr],bp      ; Save 8x8 Font address in FontPtr table
  88.   mov  word ptr [F8x8Ptr+2],es
  89.  
  90.   mov  word ptr [_FontPtr],bp     ; Default font = 8x8 ROM font
  91.   mov  word ptr [_FontPtr+2],es
  92.  
  93.   pop  ax                         ; Recall Video interrupt function parameters
  94.   mov  bh,2                       ; Select 8x14 VGA ROM font
  95.   int  10h                        ; Call BIOS video interrupt
  96.   mov  word ptr [F8x14Ptr],bp     ; Save 8x14 Font address in FontPtr table
  97.   mov  word ptr [F8x14Ptr+2],es
  98.  
  99.  
  100.   mov  al,8
  101.   mov  [_CharHeight],al            ; Set the font character heights
  102.   mov  [_CharWidth] ,al            ; Set the font character widths
  103.  
  104.   mov  dx,offset MirrorTable       ; Initialize mirror table offset
  105.   mov  [MirrorTableOffs],dx
  106.  
  107.   pop  bp
  108.   ret
  109. _x_text_init endp
  110.  
  111.  
  112. ;----------------------------------------------------------------------
  113. ; x_set_font - Mode X Set current font for text drawing
  114. ;
  115. ; C caller:
  116. ;
  117. ;  x_set_font(int FontID)
  118. ;
  119. ; PARAMETERS  FontID    0 = VGA ROM 8x8
  120. ;                       1 = VGA ROM 8x14
  121. ;                       2 = User defined bitmapped font
  122. ;
  123. ;
  124. ; WARNING: A user font must be registered before setting FontID 2
  125. ;
  126. ; Written by Themie Gouthas
  127. ;----------------------------------------------------------------------
  128.  
  129. _x_set_font proc
  130.   ARG FontID:word
  131.   push bp
  132.   mov  bp,sp
  133.  
  134.   xor  dx,dx             ; Clear DX - Mirror table offset (0 for non ROM fonts)
  135.   mov  cx,FontID
  136.   cmp  cx,2
  137.  
  138.   jne  @@not_userfont     ; Do we have a user font
  139.   mov  ax,[_UserFontPtr]   ; Yes - Activate it
  140.   mov  [_FontPtr],ax
  141.  
  142.   mov  ax,[_UserFontPtr+2]
  143.   mov  [_FontPtr+2],ax
  144.  
  145.   mov  al,[_UserChHeight]
  146.   mov  [_CharHeight],al   ; Set the font character heights
  147.  
  148.   mov  al,[_UserChWidth]
  149.   mov  [_CharWidth],al    ; Set the font character heights
  150.  
  151.   mov  al,[_UserFirstCh]
  152.   mov  [_FirstChar],al
  153.   jmp  short @@done
  154.  
  155. @@not_userfont:              ; We have a ROM font
  156.  
  157.   mov  dx,offset MirrorTable
  158.   mov  [_CharWidth],8        ; Set the font character widths
  159.   mov  [_FirstChar],0        ; Character sets start at ascii 0
  160.   cmp  cx,1                  ; Do we have an 8x14 ROM font
  161.   jne  @@not_8x14font        ; No, we have 8x8 - jump
  162.  
  163.   mov  ax,[F8x14Ptr]         ; Yes Activate it
  164.   mov  [_FontPtr],ax
  165.  
  166.   mov  ax,[F8x14Ptr+2]
  167.   mov  [_FontPtr+2],ax
  168.  
  169.   mov  [_CharHeight],14    ; Set the font character heights
  170.   jmp  short @@done
  171.  
  172. @@not_8x14font:
  173.   mov  ax,[F8x8Ptr]        ; Activate the 8x8 ROM Font
  174.   mov  [_FontPtr],ax
  175.  
  176.   mov  ax,[F8x8Ptr+2]
  177.   mov  [_FontPtr+2],ax
  178.  
  179.   mov  [_CharHeight],8     ; Set the font character heights
  180.  
  181. @@done:
  182.   mov  [MirrorTableOffs],dx
  183.  
  184.   pop  bp
  185.   ret
  186. _x_set_font endp
  187.  
  188.  
  189. ;----------------------------------------------------------------------
  190. ; x_register_userfont - Mode X register user font
  191. ;
  192. ; C caller:
  193. ;
  194. ;  x_register_userfont(void far *user_font)
  195. ;
  196. ;
  197. ; NOTES  registering a user font deregisters the previous user font
  198. ;        User fonts may be at most 8 pixels wide
  199. ;
  200. ;
  201. ; USER FONT STRUCTURE
  202. ;
  203. ;  Word:  ascii code of first char in font
  204. ;  Byte:  Height of chars in font
  205. ;  Byte:  Width of chars in font
  206. ;  n*h*Byte: the font data where n = number of chars and h = height
  207. ;      of chars
  208. ;
  209. ; WARNING: The onus is on the program to ensure that all characters
  210. ;          drawn whilst this font is active, are within the range of
  211. ;          characters defined.
  212. ;
  213. ;
  214. ; UPDATE: Variable width fonts are now available (up to 8 pixels max)
  215. ;  If the Width byte in the font header is 0 then it is assumed that
  216. ;  the font is variable width. For variable width fonts each characters
  217. ;  data is followed by one byte representing the characters pixel width.
  218. ;
  219. ; Written by Themie Gouthas
  220. ;----------------------------------------------------------------------
  221. _x_register_userfont proc
  222.   ARG  FontToRegister:dword
  223.   push bp
  224.   mov  bp,sp
  225.   push si
  226.  
  227.   mov  ax,word ptr [FontToRegister]
  228.   mov  bx,word ptr [FontToRegister+2]
  229.   add  ax,4
  230.   mov  [_UserFontPtr],ax
  231.   mov  [_UserFontPtr+2],bx
  232.  
  233.   push ds
  234.   lds  si,[FontToRegister]
  235.   lodsw
  236.   mov  bx,ax
  237.   lodsw
  238.   pop  ds
  239.  
  240.   mov  [_UserChHeight],al
  241.   mov  [_UserChWidth],ah
  242.   mov  [_UserFirstCh],bl
  243.   pop  si
  244.   pop  bp
  245.   ret
  246. _x_register_userfont endp
  247.  
  248.  
  249. _x_get_char_width  proc
  250.   ARG  Chr:byte
  251.   push bp
  252.   mov  bp,sp
  253.  
  254.   xor  ah,ah
  255.   mov  al,[_CharWidth]
  256.   or   al,al
  257.   jz   @@NotFixed
  258.   pop  bp
  259.   ret
  260.  
  261. @@NotFixed:
  262.   push si
  263.   mov  al,[_CharHeight]
  264.   mov  bx,ax
  265.   inc  al
  266.   mov  dl,[Chr]                   ; User fonts may have incomplete charsets
  267.   sub  dl,[_FirstChar]            ;  this compensates for fonts not starting at
  268.                   ;  ascii value 0
  269.   mul  dl                         ; Mult AX by character to draw giving offset
  270.                   ; of first character byte in font table
  271.   add  ax,bx
  272.   les  si,dword ptr [_FontPtr]
  273.   add  si,ax
  274.   xor  ah,ah
  275.   mov  al,es:[si]
  276.   pop  si
  277.   pop  bp
  278.   ret
  279. _x_get_char_width  endp
  280.  
  281.  
  282. ;----------------------------------------------------------------------
  283. ; x_char_put - Mode X Draw a text character at the specified location
  284. ;
  285. ;
  286. ; C caller:
  287. ;
  288. ;  x_char_put(char ch, int x, int y, unsigned ScrnOffs, unsigned Color)
  289. ;
  290. ; PARAMETERS  ch        char to draw
  291. ;             x,y       screen coords at which to draw ch
  292. ;             ScrnOffs  Starting offset of page on whih to draw
  293. ;          Color     Color of the text
  294. ;
  295. ; NOTES:  Uses the current font settings. See SetFont, InitTextDriver,
  296. ;         RegisterUserFont
  297. ;
  298. ; WARNING: InitTextDriver must be called before using this function
  299. ;
  300. ;
  301. ; Written by Themie Gouthas
  302. ;----------------------------------------------------------------------
  303. _x_char_put  proc
  304.   ARG  Chr:byte,X:word,Y:word,ScrnOffs:word,Color:word
  305.   LOCAL ScreenInc:word,Hold:word=LocalStk
  306.   push bp
  307.   mov  bp,sp
  308.   sub  sp,LocalStk
  309.   push si
  310.   push di
  311.   push ds
  312.  
  313.   cld
  314.   mov  ax,[_ScrnLogicalByteWidth] ; AX = Virtual screen width
  315.   mov  bx,ax                      ; copy Virt screen width and decrement
  316.   sub  bx,3                       ; by the max number of bytes (whole or part)
  317.                   ; that a character row may occupy on the screen
  318.   mov  [ScreenInc],bx             ; Save it to the local stack var. SceenInc
  319.   mul  [Y]                        ; Find the starting dest. screen address of
  320.   mov  di,[X]                     ;  the character to draw
  321.   mov  cx,di
  322.   shr  di,2
  323.   add  di,ax
  324.   add  di,[ScrnOffs]              ; Dont forget to compensate for page
  325.  
  326.   mov  ax,SCREEN_SEG              ; ES:DI -> first screen dest. byte of char
  327.   mov  es,ax
  328.  
  329.   and  cx,3                       ; CH = 0, CL = Plane of first pixel
  330.  
  331.   mov  bx,[MirrorTableOffs]       ; set BX to offset of mirror table for XLAT
  332.   mov  al,[_CharHeight]           ; AL = Character height, AH = 0
  333.   xor  ah,ah
  334.   mov  ch,al                      ; CH = Character height
  335.  
  336.   cmp  [_CharWidth],0
  337.   jne  @@NoWidthByte
  338.   inc  al
  339. @@NoWidthByte:
  340.  
  341.   mov  dl,[Chr]                   ; User fonts may have incomplete charsets
  342.   sub  dl,[_FirstChar]            ;  this compensates for fonts not starting at
  343.                   ;  ascii value 0
  344.   mul  dl                         ; Mult AX by character to draw giving offset
  345.                   ; of first character byte in font table
  346.  
  347.   lds  si,dword ptr [_FontPtr]    ; DS:SI -> beggining of required font
  348.   add  si,ax                      ; DS:SI -> first byte of req. char
  349.  
  350.   mov  dx,SC_INDEX                ; Prepare for VGA out's
  351.  
  352. @@MainLoop:
  353.  
  354.   lodsb               ; load character byte into AL
  355.   or   al,al
  356.   jz   @@NoCharPixels ; Dont bother if no pixels to draw
  357.  
  358.   or   bx,bx          ; if BX=0 -> User font, so no need to mirror data
  359.   jz   @@DontMirror
  360.   push ds
  361.   mov  dx,@data       ; Set DS to the Mirror lookup table's segment
  362.   mov  ds,dx          ; - BX should already contain the offset addr of table
  363.   xlatb               ; AL is now replaced by the corresponding table entry
  364.   pop  ds             ; Restore previous data segment
  365.   mov  dx,SC_INDEX    ; Restore DX
  366.  
  367. @@DontMirror:
  368.   xor  ah,ah          ; shift the byte for the dest plane and save it
  369.   shl  ax,cl
  370.   mov  [Hold],ax
  371.  
  372.   mov  ah,al                 ; output high nibble of first byte of shifted char
  373.   and  ah,0fh                ; 4 pixels at a time !
  374.   jnz  @@p1                  ; if nibble has pixels, draw them
  375.   inc  di                    ;  otherwise go to next nibble
  376.   jmp  @@SecondNibble
  377.  
  378. @@p1:
  379.   mov  al,MAP_MASK
  380.   out  dx,ax
  381.   mov  al,byte ptr [Color]
  382.   stosb
  383.  
  384. @@SecondNibble:
  385.                  ; output low nibble of first byte of shifted char
  386.   mov  ax,[Hold]
  387.   shl  ax,4
  388.   and  ah,0fh
  389.   jnz  @@p2
  390.   inc  di
  391.   jmp  @@ThirdNibble
  392.  
  393. @@p2:
  394.   mov  al,MAP_MASK
  395.   out  dx,ax
  396.   mov  al,byte ptr [Color]
  397.   stosb
  398.  
  399. @@ThirdNibble:
  400.   mov  ax,[Hold]             ; output high nibble of last byte of shifted char
  401.   and  ah,0fh
  402.   jnz  @@p3
  403.   inc  di
  404.   jmp  short  @@NextCharRow
  405.  
  406. @@p3:
  407.   mov  al,MAP_MASK           ; completing the drawing of one character row
  408.   out  dx,ax
  409.   mov  al,byte ptr [Color]
  410.   stosb
  411.  
  412. @@NextCharRow:
  413.   add  di,[ScreenInc]        ; Now move to the next screen row and do the same
  414.   dec  ch                    ; any remaining character bytes
  415.   jnz  @@MainLoop
  416.  
  417. @@done:
  418.   pop  es
  419.   mov  ah,0
  420.   mov  al,es:[_CharWidth]     ; return the character width (for string fuctions
  421.   or   al,al
  422.   jnz  @@FixedSpacing         ;  using this character drawing function).
  423.   lodsb
  424. @@FixedSpacing:
  425.  
  426.   mov  bx,es
  427.   mov  ds,bx
  428.  
  429.   pop  di
  430.   pop  si
  431.   mov  sp,bp
  432.   pop  bp
  433.   ret
  434.  
  435. @@NoCharPixels:
  436.   add  di,3
  437.   add  di,[ScreenInc]        ; Now move to the next screen row and do the same
  438.   dec  ch                    ; any remaining character bytes
  439.   jnz  @@MainLoop
  440.   jmp  short @@done
  441.  
  442. _x_char_put endp
  443.  
  444.  
  445. end
  446.  
  447.